<template>
  <div class="app-container">




    <el-table v-loading="loading" :data="retirementConfigList" @selection-change="handleSelectionChange">
      <el-table-column label="阿里云AccessKey ID" align="center" prop="accesskeyId" />
      <el-table-column label="阿里云AccessKey Secret" align="center" prop="accesskeySecret" width="330" />
      <el-table-column label="短信签名" align="center" prop="smsSignName" />
      <el-table-column label="短信模板CODE" align="center" prop="smsTemplateCode" />
      <el-table-column label="短信站点endpoint " align="center" prop="smsEndpoint" />
      <el-table-column label="语音合成appkey" align="center" prop="ossAppKey" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['retirement:retirementConfig:edit']"
          >修改</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改参数配置对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="40%" append-to-body class="config-dialog">
      <el-form ref="form" :model="form" :rules="rules" label-width="160px" class="config-form">
        <el-form-item label="AccessKey ID" prop="accesskeyId">
          <div class="form-item-wrapper">
            <el-input
              v-model="form.accesskeyId"
              placeholder="请输入阿里云AccessKey ID"
              :disabled="!editableFields.accesskeyId"
              class="form-input"
            />
            <el-button
              v-if="!editableFields.accesskeyId"
              type="text"
              icon="el-icon-edit"
              @click="enableEdit('accesskeyId')"
              class="edit-btn"
            >修改</el-button>
          </div>
        </el-form-item>

        <el-form-item label="AccessKey Secret" prop="accesskeySecret">
          <div class="form-item-wrapper">
            <el-input
              v-model="form.accesskeySecret"
              placeholder="请输入阿里云AccessKey Secret"
              :disabled="!editableFields.accesskeySecret"
              class="form-input"
            />
            <el-button
              v-if="!editableFields.accesskeySecret"
              type="text"
              icon="el-icon-edit"
              @click="enableEdit('accesskeySecret')"
              class="edit-btn"
            >修改</el-button>
          </div>
        </el-form-item>

        <el-form-item label="短信签名" prop="smsSignName">
          <div class="form-item-wrapper">
            <el-input
              v-model="form.smsSignName"
              placeholder="请输入短信签名"
              :disabled="!editableFields.smsSignName"
              class="form-input"
            />
            <el-button
              v-if="!editableFields.smsSignName"
              type="text"
              icon="el-icon-edit"
              @click="enableEdit('smsSignName')"
              class="edit-btn"
            >修改</el-button>
          </div>
        </el-form-item>

        <el-form-item label="短信模板CODE" prop="smsTemplateCode">
          <div class="form-item-wrapper">
            <el-input
              v-model="form.smsTemplateCode"
              placeholder="请输入短信模板CODE"
              :disabled="!editableFields.smsTemplateCode"
              class="form-input"
            />
            <el-button
              v-if="!editableFields.smsTemplateCode"
              type="text"
              icon="el-icon-edit"
              @click="enableEdit('smsTemplateCode')"
              class="edit-btn"
            >修改</el-button>
          </div>
        </el-form-item>

        <el-form-item label="短信站点endpoint" prop="smsEndpoint">
          <div class="form-item-wrapper">
            <el-input
              v-model="form.smsEndpoint"
              placeholder="请输入短信站点endpoint"
              :disabled="!editableFields.smsEndpoint"
              class="form-input"
            />
            <el-button
              v-if="!editableFields.smsEndpoint"
              type="text"
              icon="el-icon-edit"
              @click="enableEdit('smsEndpoint')"
              class="edit-btn"
            >修改</el-button>
          </div>
        </el-form-item>

        <el-form-item label="语音合成appkey" prop="ossAppKey">
          <div class="form-item-wrapper">
            <el-input
              v-model="form.ossAppKey"
              placeholder="请输入语音合成appkey"
              :disabled="!editableFields.ossAppKey"
              class="form-input"
            />
            <el-button
              v-if="!editableFields.ossAppKey"
              type="text"
              icon="el-icon-edit"
              @click="enableEdit('ossAppKey')"
              class="edit-btn"
            >修改</el-button>
          </div>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listRetirementConfig, getRetirementConfig, delRetirementConfig, addRetirementConfig, updateRetirementConfig } from "@/api/retirement/retirementConfig"

export default {
  name: "RetirementConfig",
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 参数配置表格数据
      retirementConfigList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        accesskeyId: null,
        accesskeySecret: null,
        smsSignName: null,
        smsTemplateCode: null,
        smsEndpoint: null,
        ossAppKey: null
      },
      // 表单参数
      form: {},
      // 可编辑字段状态
      editableFields: {
        accesskeyId: false,
        accesskeySecret: false,
        smsSignName: false,
        smsTemplateCode: false,
        smsEndpoint: false,
        ossAppKey: false
      },
      // 表单校验
      rules: {
        accesskeyId: [
          { required: true, message: "阿里云AccessKey ID不能为空", trigger: "blur" }
        ],
        accesskeySecret: [
          { required: true, message: "阿里云AccessKey Secret不能为空", trigger: "blur" }
        ],
        smsSignName: [
          { required: true, message: "短信签名不能为空", trigger: "blur" }
        ],
        smsTemplateCode: [
          { required: true, message: "短信模板CODE不能为空", trigger: "blur" }
        ],
        smsEndpoint: [
          { required: true, message: "短信站点endpoint不能为空", trigger: "blur" }
        ],
        ossAppKey: [
          { required: true, message: "语音合成appkey不能为空", trigger: "blur" }
        ]
      }
    }
  },
  created() {
    this.getList()
  },
  methods: {
    /** 查询参数配置列表 */
    getList() {
      this.loading = true
      listRetirementConfig(this.queryParams).then(response => {
        this.retirementConfigList = response.rows
        this.total = response.total
        this.loading = false
      })
    },
    // 取消按钮
    cancel() {
      this.open = false
      this.reset()
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        accesskeyId: null,
        accesskeySecret: null,
        smsSignName: null,
        smsTemplateCode: null,
        smsEndpoint: null,
        ossAppKey: null
      }
      // 重置可编辑状态
      this.editableFields = {
        accesskeyId: false,
        accesskeySecret: false,
        smsSignName: false,
        smsTemplateCode: false,
        smsEndpoint: false,
        ossAppKey: false
      }
      this.resetForm("form")
    },

    // 启用字段编辑
    enableEdit(fieldName) {
      this.$set(this.editableFields, fieldName, true)
    },

    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },

    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset()
      const id = row.id || this.ids
      getRetirementConfig(id).then(response => {
        this.form = response.data
        this.open = true
        this.title = "修改参数配置（确认前请勿随意改动！）"
      })
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updateRetirementConfig(this.form).then(response => {
              this.$modal.msgSuccess("修改成功")
              this.open = false
              this.getList()
            })
          } else {
            addRetirementConfig(this.form).then(response => {
              this.$modal.msgSuccess("新增成功")
              this.open = false
              this.getList()
            })
          }
        }
      })
    },
  }
}
</script>

<style scoped>
.config-dialog {
  border-radius: 8px;
}

.config-dialog .el-dialog__header {
  background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
  color: white;
  border-radius: 8px 8px 0 0;
  padding: 20px 24px;
}

.config-dialog .el-dialog__title {
  color: white;
  font-size: 18px;
  font-weight: 600;
}

.config-dialog .el-dialog__headerbtn .el-dialog__close {
  color: white;
  font-size: 20px;
}

.config-dialog .el-dialog__body {
  padding: 30px 24px;
  background: #fafbfc;
}

.config-form {
  background: white;
  border-radius: 8px;
  padding: 24px;
  box-shadow: 0 2px 8px rgba(0, 0, 0, 0.06);
}

.config-form .el-form-item {
  margin-bottom: 24px;
}

.config-form .el-form-item__label {
  font-weight: 600;
  color: #303133;
  font-size: 14px;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
  line-height: 1.4;
}

.form-item-wrapper {
  display: flex;
  align-items: center;
  gap: 12px;
}

.form-input {
  flex: 1;
}

.form-input .el-input__inner {
  border-radius: 6px;
  border: 1px solid #dcdfe6;
  transition: all 0.3s;
  height: 40px;
  line-height: 40px;
  padding: 0 15px;
}

.form-input .el-input__inner:focus {
  border-color: #409eff;
  box-shadow: 0 0 0 2px rgba(64, 158, 255, 0.1);
}

.form-input .el-input.is-disabled .el-input__inner {
  background-color: #f5f7fa;
  border-color: #e4e7ed;
  color: #c0c4cc;
  cursor: not-allowed;
}

.edit-btn {
  color: #409eff;
  font-size: 14px;
  padding: 8px 12px;
  border-radius: 4px;
  transition: all 0.3s;
  white-space: nowrap;
}

.edit-btn:hover {
  background-color: #ecf5ff;
  color: #66b1ff;
}

.edit-btn .el-icon-edit {
  margin-right: 4px;
}

.dialog-footer {
  text-align: right;
  padding: 10px 20px 20px;
}
</style>
